package com.rt.tmpt.service.impl;


import com.rt.tmpt.bean.A1LoginFailure;
import com.rt.tmpt.bean.A2User;
import com.rt.tmpt.dao.intf.IA1LoginFailureDao;
import com.rt.tmpt.dao.intf.IA2UserDao;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.ILoginFailureService;
import com.rt.tmpt.utils.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;


@Service
@Transactional(propagation = Propagation.REQUIRED)
@Slf4j
public class LoginFailureService implements ILoginFailureService {


    @Resource
    private IA1LoginFailureDao loginFailureDao;
    @Resource
    private IA2UserDao userDao;


    public Result loginBefore(Object entity, int hourInt, int limitFailureTimes) {
        Result result = new Result();

        A1LoginFailure loginFailure = null;
        if (entity instanceof A2User) {
            A2User emp = ((A2User) entity);
            loginFailure = emp.a1LoginFailure;
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }
        //
        if (loginFailure == null) {
            return result.success();
        }
        if (loginFailure.failureTimes == 0) {
            return result.success();
        }

        //判断时间间隔，如果可以重新登录的话，将相关数据清空(times, lasttime)
        Timestamp failureTime = loginFailure.lastFailureTime;
        String failureTimeStr = DateTool.date2String(failureTime, "yyyy-MM-dd HH:mm:ss");
        try {
            //可以解除限制了，时间过了
            if (DateTool.compare2CurDate(failureTimeStr, hourInt) == -1) {
                this.loginRelease(entity);
                return result.success();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (loginFailure.failureTimes >= limitFailureTimes) {
            try {
                return result.fault("您的帐号已被锁定，请在下列时间之后登录：" + DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime, "yyyy-MM-dd HH:mm:ss"), hourInt));
            } catch (ParseException e) {
                e.printStackTrace();
                return result.fault("您的帐号已被锁定");
            }
        }

        return result.success();
    }

    public Result loginFailure(Object entity, int hourInt, int limitFailureTimes) {
        Result result = new Result();
        StringBuilder sb = new StringBuilder();
        A1LoginFailure loginFailure = null;

        if (entity instanceof A2User) {
            A2User emp = ((A2User) entity);
            loginFailure = emp.a1LoginFailure;
            if (loginFailure == null) {
                loginFailure = new A1LoginFailure();
                loginFailure.loginName = emp.getLoginName();
                loginFailure.failureTimes = 1;
                loginFailure.lastFailureTime = DateTool.getCurTimestamp();
                loginFailure.a2User = emp;
                loginFailureDao.save(loginFailure);
                return result.success();
            }
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }

        loginFailure.failureTimes = loginFailure.failureTimes + 1;
        loginFailure.lastFailureTime = DateTool.getCurTimestamp();
        loginFailureDao.save(loginFailure);

        if (loginFailure.failureTimes < 3) {
            return result.success();
        }

        if (loginFailure.failureTimes >= 3) {
            int count = limitFailureTimes - loginFailure.failureTimes;
            if (count == 0) {
                sb.setLength(0);
                try {
                    sb.append("您的帐号已被锁定，请在下列时间之后登录：" + DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime, "yyyy-MM-dd HH:mm:ss"), hourInt));
                } catch (ParseException e) {
                    sb.append("您的帐号已被锁定");
                }
            } else {
                sb.setLength(0);
                sb.append("您还有").append(count).append("次登录机会,");
                sb.append("共").append(limitFailureTimes).append("次登录密码错误后，您的帐号将被锁定");
                sb.append(hourInt).append("小时");
            }
            return result.success(sb.toString());
        }


        return result.success();
    }

    public Result loginRelease(Object entity) {
        Result result = new Result();

        A1LoginFailure loginFailure = null;
        if (entity instanceof A2User) {
            A2User emp = ((A2User) entity);
            loginFailure = emp.a1LoginFailure;
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }
        //never failed , no data in LoginFailure
        if (loginFailure == null) {
            return result.success();
        }

        loginFailure.failureTimes = 0;
        loginFailure.lastFailureTime = null;
        loginFailureDao.save(loginFailure);

        return result.success();
    }


    @Override
    public Result loginBefore(long customerId, long empId, long merchantInfoId, int hourInt,
                              int limitFailureTimes) {
        Object entity = null;
        if (empId != 0) {
            entity = userDao.findById(empId);
        }
        return this.loginBefore(entity, hourInt, limitFailureTimes);
    }

    @Override
    public Result loginFailure(long customerId, long empId, long merchantInfoId, int hourInt,
                               int limitFailureTimes) {
        Object entity = null;
        if (empId != 0) {
            entity = userDao.findById(empId);
        }

        return this.loginFailure(entity, hourInt, limitFailureTimes);
    }

    @Override
    public Result loginRelease(long customerId, long empId, long merchantInfoId) {
        Object entity = null;
        if (empId != 0) {
            entity = userDao.findById(empId);
        }
        return this.loginRelease(entity);
    }


    public Result loginBefore(Object customer, A2User emp, int hourInt, int limitFailureTimes) {
        Result result = new Result();

        A1LoginFailure loginFailure = null;
        if (emp != null) {
            loginFailure = emp.a1LoginFailure;
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }
        //
        if (loginFailure == null) {
            return result.success();
        }
        if (loginFailure.failureTimes == 0) {
            return result.success();
        }

        //判断时间间隔，如果可以重新登录的话，将相关数据清空(times, lasttime)
        Timestamp failureTime = loginFailure.lastFailureTime;
        String failureTimeStr = DateTool.date2String(failureTime, "yyyy-MM-dd HH:mm:ss");
        try {
            //可以解除限制了，时间过了
            if (DateTool.compare2CurDate(failureTimeStr, hourInt) == -1) {
                this.loginRelease(customer, emp);
                return result.success();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (loginFailure.failureTimes >= limitFailureTimes) {
            try {
                return result.fault("您的帐号已被锁定，请在下列时间之后登录：" + DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime, "yyyy-MM-dd HH:mm:ss"), hourInt));
            } catch (ParseException e) {
                e.printStackTrace();
                return result.fault("您的帐号已被锁定");
            }
        }

        return result.success();
    }

    public Result loginFailure(Object customer, A2User emp, int hourInt, int limitFailureTimes) {
        Result result = new Result();
        StringBuilder sb = new StringBuilder();
        A1LoginFailure loginFailure = null;

        if (emp != null) {
            loginFailure = emp.a1LoginFailure;
            if (loginFailure == null) {
                loginFailure = new A1LoginFailure();
                loginFailure.loginName = emp.getLoginName();
                loginFailure.failureTimes = 1;
                loginFailure.lastFailureTime = DateTool.getCurTimestamp();
                loginFailure.a2User = emp;
                loginFailureDao.save(loginFailure);
                return result.success();
            }
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }


        loginFailure.failureTimes = loginFailure.failureTimes + 1;
        loginFailure.lastFailureTime = DateTool.getCurTimestamp();
        loginFailureDao.save(loginFailure);

        if (loginFailure.failureTimes < 3) {
            return result.success();
        }

        if (loginFailure.failureTimes >= 3) {
            int count = limitFailureTimes - loginFailure.failureTimes;
            if (count == 0) {
                sb.setLength(0);
                try {
                    sb.append("您的帐号已被锁定，请在下列时间之后登录：" + DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime, "yyyy-MM-dd HH:mm:ss"), hourInt));
                } catch (ParseException e) {
                    sb.append("您的帐号已被锁定");
                }
            } else {
                sb.setLength(0);
                sb.append("您还有").append(count).append("次登录机会,");
                sb.append("共").append(limitFailureTimes).append("次登录密码错误后，您的帐号将被锁定");
                sb.append(hourInt).append("小时");
            }
            return result.success(sb.toString());
        }


        return result.success();
    }

    public Result loginRelease(Object customer, A2User emp) {
        Result result = new Result();

        A1LoginFailure loginFailure = null;
        if (emp != null) {
            loginFailure = emp.a1LoginFailure;
        } else {
            return result.fault("登录次数限制:传入参数无效");
        }
        //never failed , no data in LoginFailure
        if (loginFailure == null) {
            return result.success();
        }

        loginFailure.failureTimes = 0;
        loginFailure.lastFailureTime = null;
        loginFailureDao.save(loginFailure);

        return result.success();
    }


}
